org.piratis.j2me.core.game
Class Sprite

java.lang.Object
  extended byorg.piratis.j2me.core.game.Layer
      extended byorg.piratis.j2me.core.game.Sprite

public class Sprite
extends Layer

Provide some basic sprite functionality: getting and setting of coordinates and dimensions, as well as simple collision detection.

Version:
$Id: Sprite.java,v 1.4 2004/07/01 22:17:51 piratis Exp $
Author:
Jordi Martín

Field Summary
static int NO_FRAME
          Current frame will return this if there is no animated sprite
 
Fields inherited from class org.piratis.j2me.core.game.Layer
bbox, TOP_LEFT, visible
 
Constructor Summary
Sprite(Image img)
          New 'static' Sprite with the given Image.
Sprite(Image img, int frameWidth, int frameHeight)
          New 'dynamic' (animtaed) Sprite with the given frames contained in the Image.
 
Method Summary
 int getFrame()
          Gets the current frame index
 void nextFrame()
          Moves forward to the next frame in sequence

Frame sequences are circular: when reached the end of the sequence, it will continue at the beginning.
 void paint(Graphics g)
          Paints the Sprite into the Graphics object provided.
 void prevFrame()
          Moves backward to the previous frame in sequence

Frame sequences are circular: when reached the beginnig of the sequence, it will continue at the last frame.
 void setFrame(int newFrame)
          Selectes the current frame in the current sequence.
 void setFrameSequence(int[] sequence)
          Establish the frame sequence of the Sprite.
 
Methods inherited from class org.piratis.j2me.core.game.Layer
getBbox, isVisible, moveBy, moveTo, setVisible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_FRAME

public static int NO_FRAME
Current frame will return this if there is no animated sprite

Constructor Detail

Sprite

public Sprite(Image img)
New 'static' Sprite with the given Image.


Sprite

public Sprite(Image img,
              int frameWidth,
              int frameHeight)
New 'dynamic' (animtaed) Sprite with the given frames contained in the Image. Each frame has the same size (frameWidth x frameHeight). The Image provided might conform whatever layout, but its overall width and height must be a an integer multiple of the frame width and height.

Aside from that, the Sprite will behave as a static Sprite regarding its width and height (by calling Layer.getBbox()).

Parameters:
img - Image containing the frames
frameWidth - the width of each frame
frameHeight - the height of each frame
Method Detail

paint

public final void paint(Graphics g)
Paints the Sprite into the Graphics object provided.

Specified by:
paint in class Layer
Parameters:
g - the graphics object for rendering this layer
See Also:
Layer#paint(javax.microedition.lcdui.Graphics)

getFrame

public int getFrame()
Gets the current frame index

Returns:
the index in the frame sequence

setFrame

public void setFrame(int newFrame)
              throws java.lang.IndexOutOfBoundsException
Selectes the current frame in the current sequence.

The index refers to the position in the frame sequence, not to the frame index (within the Image).

Parameters:
newFrame - the index of the desired frame to be displayed on the next paint call of the current sequence
Throws:
java.lang.IndexOutOfBoundsException - if the index is out of the bound of the current frame sequence

nextFrame

public void nextFrame()
Moves forward to the next frame in sequence

Frame sequences are circular: when reached the end of the sequence, it will continue at the beginning.

See Also:
prevFrame()

prevFrame

public void prevFrame()
Moves backward to the previous frame in sequence

Frame sequences are circular: when reached the beginnig of the sequence, it will continue at the last frame.

See Also:
nextFrame()

setFrameSequence

public void setFrameSequence(int[] sequence)
Establish the frame sequence of the Sprite.

Parameters:
sequence -

Copyright(c) 2003-2004 Jordi Martin